﻿
''' <summary>
''' ユーザー管理
''' </summary>
''' <remarks></remarks>
Public Class Users
	Inherits UI.CommonPage

#Region " Declare "

	''' <summary>
	''' ユーザーテーブル列情報
	''' </summary>
	''' <remarks></remarks>
	Private Enum _userCol As Integer
		Actions
		Id
		Name
		Mail
		Role
		Groups
	End Enum

	''' <summary>DAO</summary>
	Protected dao As Db.IDaoUser

#End Region

#Region " Handles "

	''' <summary>
	''' ページロード
	''' </summary>
	''' <param name="sender"></param>
	''' <param name="e"></param>
	''' <remarks></remarks>
	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		_dataBind()
	End Sub

	''' <summary>
	''' テーブル行バインド
	''' </summary>
	''' <param name="sender"></param>
	''' <param name="e"></param>
	''' <remarks></remarks>
	Private Sub grdvUsers_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles grdvUsers.RowDataBound
		' 現在行がデータ行以外は処理無し
		If e.Row.RowType <> DataControlRowType.DataRow Then
			Return
		End If

		Dim cbo As DropDownList
		cbo = e.Row.Cells(_userCol.Role).FindControl("cboRole")
		If cbo IsNot Nothing Then
			_cboBindRole(cbo)
			cbo.SelectedValue = DirectCast(e.Row.DataItem, UserEntity).Role
		End If
	End Sub

	''' <summary>
	''' テーブル行コマンド
	''' </summary>
	''' <param name="sender"></param>
	''' <param name="e"></param>
	''' <remarks></remarks>
	Private Sub grdvUsers_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles grdvUsers.RowCommand
		Select Case e.CommandName
			Case "AddNew"
				_validAdd(e)
				_grdvCommandAddNew(e)
		End Select
	End Sub

	''' <summary>
	''' テーブル行編集
	''' </summary>
	''' <param name="sender"></param>
	''' <param name="e"></param>
	''' <remarks></remarks>
	Private Sub grdvUsers_RowEditing(sender As Object, e As GridViewEditEventArgs) Handles grdvUsers.RowEditing
		Me.grdvUsers.EditIndex = e.NewEditIndex
		_dataBind()
	End Sub

	''' <summary>
	''' テーブル行削除
	''' </summary>
	''' <param name="sender"></param>
	''' <param name="e"></param>
	''' <remarks></remarks>
	Private Sub grdvUsers_RowDeleting(sender As Object, e As GridViewDeleteEventArgs) Handles grdvUsers.RowDeleting
		_grdvCommandDelete(e)
	End Sub

	''' <summary>
	''' テーブル更新
	''' </summary>
	''' <param name="sender"></param>
	''' <param name="e"></param>
	''' <remarks></remarks>
	Private Sub grdvUsers_RowUpdating(sender As Object, e As GridViewUpdateEventArgs) Handles grdvUsers.RowUpdating
		_validRowUpdating(e)
		If Not Me.IsValid Then
			Return
		End If

		_grdvCommandModify(e)
	End Sub

	''' <summary>
	''' テーブル編集キャンセル
	''' </summary>
	''' <param name="sender"></param>
	''' <param name="e"></param>
	''' <remarks></remarks>
	Private Sub grdvUsers_RowCancelingEdit(sender As Object, e As GridViewCancelEditEventArgs) Handles grdvUsers.RowCancelingEdit
		Me.grdvUsers.EditIndex = -1
		_dataBind()
	End Sub

#End Region

#Region " Methods "

	''' <summary>
	''' テーブルとデータのバインド
	''' </summary>
	''' <returns></returns>
	''' <remarks></remarks>
	Private Function _dataBind() As Integer
		Dim lst As IList(Of UserEntity)

		lst = dao.GetAll()

		Me.grdvUsers.DataSource = lst
		Me.grdvUsers.DataBind()

		Dim cbo As DropDownList
		cbo = Me.grdvUsers.FooterRow.Cells(_userCol.Role).FindControl("cboRoleNew")
		_cboBindRole(cbo)

		Return lst.Count
	End Function

	Private Sub _cboBindRole(ByVal cbo As DropDownList)
		Dim lst As Moca.ConstantDataSet.ConstantDataTable
		lst = daoCommon.GetRole()
		Me.bindComboBox(cbo, lst.ValueColumn.ColumnName, lst.TextColumn.ColumnName, lst)
	End Sub

	''' <summary>
	''' ユーザー追加
	''' </summary>
	''' <param name="e"></param>
	''' <remarks></remarks>
	Private Sub _grdvCommandAddNew(ByVal e As GridViewCommandEventArgs)
		If Not Me.IsValid Then
			Return
		End If

		Dim val As UserEntity
		val = New UserEntity
		val.Name = DirectCast(Me.grdvUsers.FooterRow.Cells(0).FindControl("txtUserAdd"), TextBox).Text

		dao.Add(val)

		' データ再取得
		Me.grdvUsers.EditIndex = -1
		_dataBind()
	End Sub

	''' <summary>
	''' ユーザー削除
	''' </summary>
	''' <param name="e"></param>
	''' <remarks></remarks>
	Private Sub _grdvCommandDelete(ByVal e As GridViewDeleteEventArgs)
		Dim id As Integer
		id = Me.grdvUsers.DataKeys(e.RowIndex).Value

		' 変更キー値設定
		Dim val As UserEntity
		val = New UserEntity
		val.Id = id

		' 変更
		dao.Del(val)

		' データ再取得
		Me.grdvUsers.EditIndex = -1
		_dataBind()
	End Sub

	''' <summary>
	''' ユーザー変更
	''' </summary>
	''' <param name="e"></param>
	''' <remarks></remarks>
	Private Sub _grdvCommandModify(ByVal e As GridViewUpdateEventArgs)

		Dim id As Integer
		id = Me.grdvUsers.DataKeys(e.RowIndex).Value

		' 変更キー値設定
		Dim val As UserEntity
		val = New UserEntity
		val.Id = id
		val.Name = _getTextBoxInGrdvRow(e.RowIndex, _userCol.Name, "txtName").Text

		' 変更
		dao.Upd(val)

		' データ再取得
		Me.grdvUsers.EditIndex = -1
		_dataBind()
	End Sub

	''' <summary>
	''' 追加時の検証
	''' </summary>
	''' <param name="e"></param>
	''' <remarks></remarks>
	Private Sub _validAdd(ByVal e As GridViewCommandEventArgs)
		'Dim txt As TextBox
		'Dim name As String
		'Dim rc As Moca.Util.ValidateTypes
		Dim val As New UserEntity

		Me.validUser.IsValid = False

		' 基本的な検証とエンティティへの退避
		Me.validUser.IsValid = UpdateEntity(Me, val, AddressOf _validAddEntity)

		'txt = DirectCast(Me.grdvUsers.FooterRow.Cells(0).FindControl("txtUserAdd"), TextBox)
		'name = txt.Text

		'rc = validator.Verify(name, Moca.Util.ValidateTypes.Required)
		'If Not validator.IsValidRequired(rc) Then
		'	Me.setErrorMessage("hoge", validUser, New WebControl() {txt}, name, rc)
		'	Return
		'End If

		If Not Me.validUser.IsValid Then
			Return
		End If

		'_val = val

		Me.validUser.IsValid = True
	End Sub

	''' <summary>
	''' 検証
	''' </summary>
	''' <param name="sender"></param>
	''' <param name="e"></param>
	''' <remarks>
	''' 相関チェックなど追加のチェックを行う
	''' </remarks>
	Private Sub _validAddEntity(ByVal sender As Object, ByVal e As Moca.UpdateEntityValidateArgs)
		Dim webCtrl() As WebControl = New WebControl() {sender}

		'' 宛先
		'If e.Caption.Equals(Sys.Captions.ToUser) Then
		'	webCtrl = New WebControl() {Me.divTo}
		'	If Not e.IsError(Sys.Captions.ToUser) Then
		'		clearControlError(webCtrl)
		'		Me.divTo.BorderWidth = New Unit(0)
		'		Return
		'	End If
		'End If
		'' ダウンロード期間
		'If e.Caption.Equals(Sys.Captions.DownloadTerm) Then
		'	If Not e.IsError(Sys.Captions.DownloadTerm) Then
		'		If e.Value > Me.MstSystemSettings.MaxDownloadTerm Then
		'			e.IsValid = False
		'			setError(Me.validSend, webCtrl, MessageIDs.E008, e.Caption, Me.MstSystemSettings.MaxDownloadTerm)
		'			Return
		'		End If
		'	End If
		'End If

		' メッセージ設定
		setErrorMessage(e.Caption, Me.validUser, webCtrl, e.Value, e.ValidateResultType, e.Min, e.Max)
	End Sub

	''' <summary>
	''' 変更時の検証
	''' </summary>
	''' <param name="e"></param>
	''' <remarks></remarks>
	Private Sub _validRowUpdating(ByVal e As GridViewUpdateEventArgs)
		Dim txt As TextBox
		Dim name As String
		Dim rc As Moca.Util.ValidateTypes

		Me.validUser.IsValid = False

		txt = _getTextBoxInGrdvRow(e.RowIndex, _userCol.Name, "txtName")
		name = txt.Text

		rc = validator.Verify(name, Moca.Util.ValidateTypes.Required)
		If Not validator.IsValidRequired(rc) Then
			Me.setErrorMessage("hoge", validUser, New WebControl() {txt}, name, rc)
			Return
		End If

		Me.validUser.IsValid = True
	End Sub

	''' <summary>
	''' グリッド上のテキストボックスを返す
	''' </summary>
	''' <param name="rowNo">行</param>
	''' <returns></returns>
	''' <remarks></remarks>
	Private Function _getTextBoxInGrdvRow(ByVal rowNo As Integer, ByVal col As _userCol, ByVal controlName As String) As TextBox
		Return CType(Me.grdvUsers.Rows(rowNo).Cells(col).FindControl(controlName), TextBox)
	End Function

#End Region

End Class
